package com.yzh.pinboard.CustomView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.media.ThumbnailUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.yzh.pinboard.BoardView.Clip;
import com.yzh.pinboard.BoardView.ClipGroup;
import com.yzh.pinboard.BoardView.PinBoardLine;
import com.yzh.pinboard.adapter.BoardAdapter;
import com.yzh.pinboard.utils.DensityUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;


public class BoardGroupView extends ViewGroup implements View.OnClickListener, NavView
        .OnNavViewClickListener,
        BoardAdapter.OnChangeListener, EraserSettingView.EraserListener, PaintSettingView
                .PaintSettingListener, MenuView.MenuListener {

    private BoardAdapter mBoardAdapter;


    private Context mContext;
    private PinBoardLine mContentView;
    private ExpandableListView mEdgeView;
    private NavView mNavView;
    private MenuView mMenuView;
    private SlideViewLayout mThumbnailEraseView;
    private int mEdgeViewWidth;
    private int mNavViewHeight;


    public BoardGroupView(Context context) {
        super(context);
        init(context);
    }

    public BoardGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public BoardGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;

        mContentView = new PinBoardLine(mContext);
        LayoutParams contentParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams
                .MATCH_PARENT);
        mContentView.setLayoutParams(contentParams);
        addView(mContentView, 0);

        mEdgeView = new ExpandableListView(context);
        mEdgeView.setBackgroundColor(0x00000000);
        mEdgeView.setGroupIndicator(null);
        mEdgeView.setSelector(new ColorDrawable(0x00000000));
        mEdgeView.setDivider(null);

        mThumbnailEraseView = new SlideViewLayout(context);
        addView(mThumbnailEraseView);

        addView(mEdgeView);

        setBoardListViewAdapter();
        mNavView = new NavView(context);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams
                .WRAP_CONTENT);
        mNavView.setLayoutParams(params);
        addView(mNavView);

        mMenuView = new MenuView(mContext);
        LayoutParams menuParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams
                .WRAP_CONTENT);
        mMenuView.setLayoutParams(menuParams);
        addView(mMenuView);

        testEdge();
    }

    private void testEdge() {
        String edArg1 = "/storage/emulated/0/DCIM/Camera/IMG_20160211_201505.jpg";
        String edArg2 = "Camera";
        String[] edArg3 = new String[]{"/storage/emulated/0/DCIM/Camera/IMG_20160211_201505.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20160211_201525.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20170322_121930.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20160211_201505.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20160211_201525.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20170322_121930.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20160211_201505.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20160211_201525.jpg",
                "/storage/emulated/0/DCIM/Camera/IMG_20170322_121930.jpg"
        };
        selectImageData(edArg1, edArg2, Arrays.asList(edArg3));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);


        LayoutParams edgeViewParams = mEdgeView.getLayoutParams();
        edgeViewParams.width = DensityUtil.dip2px(getContext(), 150);
        mEdgeView.setLayoutParams(edgeViewParams);

        LayoutParams tevViewParams = mThumbnailEraseView.getLayoutParams();
        tevViewParams.width = DensityUtil.dip2px(getContext(), 150);
        tevViewParams.height = DensityUtil.dip2px(getContext(), 30);

        mThumbnailEraseView.setLayoutParams(tevViewParams);


        mEdgeViewWidth = mEdgeView.getMeasuredWidth();
        mNavViewHeight = mNavView.getMeasuredHeight();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mContentView.layout(0, 0, mContentView.getMeasuredWidth(),
                mContentView.getMeasuredHeight());

        layoutEdgeView();

        int tevLeft = getMeasuredWidth() - mEdgeView.getMeasuredWidth() - DensityUtil.dip2px(mContext, 60);
        int tevTop = getMeasuredHeight() - DensityUtil.dip2px(mContext, 60);
        mThumbnailEraseView.layout(tevLeft, tevTop,
                tevLeft + mThumbnailEraseView.getMeasuredWidth(), tevTop + mThumbnailEraseView.getMeasuredHeight());

        mNavView.layout((getMeasuredWidth() - mNavView.getMeasuredWidth()) / 2, getMeasuredHeight
                        () - mNavViewHeight - DensityUtil.dip2px(mContext, -10),
                (getMeasuredWidth() + mNavView.getMeasuredWidth()) / 2, getMeasuredHeight() -
                        DensityUtil.dip2px(mContext, -10));
        mMenuView.layout(DensityUtil.dip2px(mContext, 20), getMeasuredHeight() - mMenuView.getMeasuredHeight()
                        - DensityUtil.dip2px(mContext, -10),
                mMenuView.getMeasuredWidth() + DensityUtil.dip2px(mContext, 20),
                getMeasuredHeight() - DensityUtil.dip2px(mContext, -10));
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
    }


    /**
     * 位移
     *
     * @param view
     * @param xTran
     * @param yTran
     */
    public void animTranslate(final View view, final int xTran, final int yTran) {
        TranslateAnimation animation = new TranslateAnimation(0, xTran, 0, yTran);
        animation.setDuration(300);
        animation.setFillAfter(true);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                int left = view.getLeft() + xTran;
                int top = view.getTop() + yTran;
                int width = view.getWidth();
                int height = view.getHeight();
                view.clearAnimation();
                view.layout(left, top, left + width, top + height);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animation);
        invalidate();
    }

    private boolean edgeViewIsShow() {
        return mEdgeView.getLeft() == getMeasuredWidth() - mEdgeViewWidth;
    }

    private boolean edgeViewIsHidden() {
        return mEdgeView.getLeft() == getMeasuredWidth();
    }

    private void setBoardListViewAdapter() {
        List<ClipGroup> parentList = new ArrayList<>();
        mBoardAdapter = new BoardAdapter(mContext, parentList);
        mEdgeView.setAdapter(mBoardAdapter);
        mBoardAdapter.setOnDeleteListener(this);
    }


    public void setNavViewClickListener(NavView.OnNavViewClickListener navViewClickListener) {
        mNavView.setNavViewClickListener(navViewClickListener);
    }

    public Bitmap shotView(View view, int width) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return ThumbnailUtils.extractThumbnail(bitmap, width, width * 9 / 16);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContentView.setOnClickListener(this);
        mNavView.setNavViewClickListener(this);
        mNavView.setEraserListener(this);
        mNavView.setPaintListener(this);
        mMenuView.setMenuListener(this);
    }


    /**
     * 选择图片数据
     *
     * @param groupPath
     * @param groupName
     * @param imgList
     */
    private void selectImageData(String groupPath, String groupName, List<String> imgList) {
        List<Clip> boardList = new ArrayList<Clip>();
        for (String imgPath : imgList) {
            Clip clip = new Clip(imgPath);
            clip.id = UUID.randomUUID().toString();
            boardList.add(clip);
        }
        boolean groupPathExist = false;
        for (ClipGroup clipGroup : mBoardAdapter.getParentList()) {
            if (clipGroup.getGroupPath().equals(groupPath)) {
                groupPathExist = true;
            }
        }
        if (!groupPathExist) {
            ClipGroup clipGroup = new ClipGroup(groupPath, groupName, boardList);
            mBoardAdapter.addData(clipGroup);
        }
        for (int i = 0; i < mBoardAdapter.getGroupCount(); i++) {
            mEdgeView.expandGroup(i);//展开group
        }
    }

//    @Override
//    public void onClickCamera() {
//        CameraOrPhotoDialog cameraOrPhotoDialog = new CameraOrPhotoDialog(mContext);
//        cameraOrPhotoDialog.show();
//        cameraOrPhotoDialog.setOnChooseListener(new CameraOrPhotoDialog.onChooseListener() {
//            @Override
//            public void chooseCamera() {
//                CameraDialog cameraDialog = new CameraDialog(mContext);
//                cameraDialog.show();
//                cameraDialog.setCameraCallBack(new CameraDialog.CameraCallBack() {
//                    @Override
//                    public void shot(Bitmap bitmap) {
//
//                    }
//                });
//            }
//
//            @Override
//            public void choosePhoto() {
//                PhotoPickerDialog photoPickerDialog = new PhotoPickerDialog(mContext);
//                photoPickerDialog.show();
//                photoPickerDialog.setOnPicSelectListener(new PhotoPickerDialog
//                        .onPicSelectListener() {
//
//
//                    @Override
//                    public void onSelectPic(int position, String path) {
//                        Toast.makeText(mContext, path, Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//        });
//
//    }


    private void layoutEdgeView() {

        int top = getMeasuredHeight() - mEdgeView.getMeasuredHeight() - DensityUtil.dip2px(mContext, 90);
        if (top < 0) {
            top = 0;
        }
        mEdgeView.layout(getMeasuredWidth() - mEdgeView.getMeasuredWidth() - DensityUtil.dip2px(mContext, 60),
                top,
                getMeasuredWidth() - DensityUtil.dip2px(mContext, 60),
                getMeasuredHeight() - DensityUtil.dip2px(mContext, 90));
    }

    @Override
    public void OnDeleteGroup(int parentPosition, ClipGroup clipGroup) {
        // TODO: 2017/3/9
    }

    @Override
    public void OnDeleteItem(int parentPosition, int childPosition, Clip clip) {
        // TODO: 2017/3/9
        layoutEdgeView();
    }

    @Override
    public void OnSelectedItem(int parentPosition, int childPosition, Clip clip) {

    }


    /**
     * 添加到最后一组尾部
     */
    public void addDataToLastGroup(Clip clip) {
        mBoardAdapter.addData(clip);
    }

    /**
     * 添加到指定组的尾部，组内单个数据
     *
     * @param parentPosition 指定组位置
     * @param clip
     */
    public void addData(int parentPosition, Clip clip) {
        mBoardAdapter.addData(parentPosition, clip);
    }

    /**
     * 添加到制定组的制定位置，组内单个数据
     *
     * @param parentPosition 指定组位置
     * @param childPosition  数据位置
     * @param clip
     */
    public void addData(int parentPosition, int childPosition, Clip clip) {
        mBoardAdapter.addData(parentPosition, childPosition, clip);
    }

    /**
     * 在尾部添加新组，组内单个数据
     *
     * @param clipGroup 文件夹
     */
    public void addData(ClipGroup clipGroup) {
        mBoardAdapter.addData(clipGroup);
    }

    /**
     * 在指定位置添加新组，组内单个数据
     *
     * @param parentPosition 指定组位置
     * @param clipGroup      文件夹
     */
    public void addData(int parentPosition, ClipGroup clipGroup) {
        mBoardAdapter.addData(parentPosition, clipGroup);
    }

    /**
     * 删除指定组中的数据
     *
     * @param parentPosition 指定组位置
     * @param clip
     */
    public void removeData(int parentPosition, Clip clip) {
        mBoardAdapter.removeData(parentPosition, clip);
    }

    /**
     * 删除指定组中的数据
     *
     * @param parentPosition 指定组位置
     * @param childPosition  数据位置
     */
    public void removeData(int parentPosition, int childPosition) {
        mBoardAdapter.removeData(parentPosition, childPosition);
    }

    /**
     * 修改指定组中的数据
     *
     * @param parentPosition 指定组位置
     * @param childPosition  数据位置
     * @param clip           新数据
     */
    public void updateData(int parentPosition, int childPosition, Clip clip) {
        mBoardAdapter.updateData(parentPosition, childPosition, clip);
    }

    @Override
    public void onUndo() {
        Toast.makeText(mContext, "上一步", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRedo() {
        Toast.makeText(mContext, "下一步", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onSelectRect() {
        Toast.makeText(mContext, "选择区域", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onEraserAll() {
        mContentView.deleteAll();
        //Toast.makeText(mContext, "擦除所有", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onEraserLine() {
        Toast.makeText(mContext, "擦除线", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onEraserRect() {
        Toast.makeText(mContext, "区域擦除", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void selectLineWidth(float width) {
        Toast.makeText(mContext, "笔宽度：" + width, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void selectColor(int color) {
        Toast.makeText(mContext, "笔颜色:" + color, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void saveBoard() {
        Toast.makeText(mContext, "保存文档", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void openBoard() {
        Toast.makeText(mContext, "打开文档", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void changeBackground() {
        Toast.makeText(mContext, "更改背景", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void closeBoard() {
        SysApplication.getInstance().exit();
    }
}
